import { getUserCoupons } from "api/couponApi"

export default {
    state: () => ({
        isCouponChosenForOrder: false,
        userCoupons: [],
        currentCoupon: null,
        userCouponsCount: 0
    }),
    getters: {
        isCouponChosenForOrder: state => state.isCouponChosenForOrder,
        userCoupons: state => state.userCoupons,
        currentCoupon: state => state.currentCoupon,
        userCouponsCount: state => state.userCouponsCount
    },
    mutations: {
        setIsCouponChosenForOrder(state, isCouponChosenForOrder = false) {
            state.isCouponChosenForOrder = isCouponChosenForOrder
        },
        setUserCoupons(state, coupons = []) {
            state.userCoupons = coupons
        },
        setCurrentCoupon(state, coupon = {}) {
            state.currentCoupon = coupon
        },
        setUserCouponsCount(state, count = 0) {
            state.userCouponsCount = count
        }
    },
    actions: {
        async updateIsCouponChosenForOrder({ commit }, isCouponChosenForOrder = false) {
            commit('setIsCouponChosenForOrder', isCouponChosenForOrder)
        },
        async getUserCoupons({ commit }) {
            const response = await getUserCoupons()
            commit('setUserCoupons', response.data)
            commit('setUserCouponsCount', response.data.length)
        },
        async updateCurrentCoupon({ commit }, coupon) {
            commit('setCurrentCoupon', coupon)
        }
    }
}
